package com.redwine.app.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * 圆图片加阴影
 * 作者：gassion on 17/7/1 08:46
 * 最可怕的敌人，就是没有坚强的信念。
 */

@SuppressLint("AppCompatCustomView")
public class CircularImageView extends ImageView {

    private int borderWidth = 14;

    public CircularImageView(Context context) {
        super(context);
        setWillNotDraw(false);
    }

    public CircularImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
    }

    public CircularImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setWillNotDraw(false);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (getDrawable() == null) return;
        Bitmap bit = ((BitmapDrawable) getDrawable()).getBitmap();
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bit, canvas.getWidth() - borderWidth, canvas.getHeight() - borderWidth, false);
        canvas.drawBitmap(getRoundedCornerBitmap(scaledBitmap), 0, 0, null);


    }

    /**
     * 将图片准转为圆形
     *
     * @param bitmap
     * @return
     */
    public Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
        int size = getWidth() - borderWidth;
        Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, size, size);
        final float roundPx = size / 2;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(roundPx, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return getRoundeBitmapWithWhite(output);
    }

    /**
     * 获取白色边框
     *
     * @param bitmap
     * @return
     */
    public Bitmap getRoundeBitmapWithWhite(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(getWidth(), getWidth(), Bitmap.Config.ARGB_8888);
        final float roundPx = getWidth() / 2;
        Canvas canvas = new Canvas(output);

        final int color = Color.parseColor("#FFFFFF");
        final Paint paint = new Paint();

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawBitmap(bitmap, borderWidth / 2, borderWidth / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));

        RadialGradient gradient = new RadialGradient(roundPx, roundPx, roundPx,
                new int[]{Color.WHITE, Color.WHITE,
                        Color.parseColor("#BBBBBB")}, new float[]{0.0f,
                0.97f, 1.0f}, Shader.TileMode.CLAMP);
        paint.setShader(gradient);
        canvas.drawCircle(roundPx, roundPx, roundPx, paint);
        return output;
    }


}